package com.ZengQi.event.dao.impl;


import com.ZengQi.event.dao.CommentDao;
import com.ZengQi.event.entity.Comment;
import com.ZengQi.event.util.SearchUtils;
import com.ZengQi.event.util.UpdateUtils;

import java.util.List;

public class CommentDaoImpl implements CommentDao {
    @Override
    public boolean insertComment(Comment comment) {
        String sql="insert into comment values(null,?,?,?)";
        boolean update = UpdateUtils.update(sql, comment.getCommentPublisher(), comment.getCommentContent(), comment.getTopicName());


        return update;
    }

    @Override
    public List<Comment> listComments(Comment conditionComment) {
        String sql="select comment_id commentId,comment_publisher commentPublisher,comment_content commentContent,topic_name topicName from comment where topic_name=?";
        List<Comment> comments = SearchUtils.searchForList(Comment.class, sql, conditionComment.getTopicName());
        if(comments.size()==0) {

            return null;
        }
        return comments;
    }

    @Override
    public boolean deleteComment(Comment conditionComment) {
        String sql="delete from comment where comment_content=? and comment_publisher=? and topic_name=?";
        boolean update = UpdateUtils.update(sql, conditionComment.getCommentContent(), conditionComment.getCommentPublisher(), conditionComment.getTopicName());
        return update;
    }

    @Override
    public boolean updateCommentByCommentPublisher(Comment comment,String oldCommentPublisher) {

        String sql="update comment set comment_publisher=? where comment_publisher=? ";
        boolean update = UpdateUtils.update(sql, comment.getCommentPublisher(), oldCommentPublisher);
        return update;



    }

    @Override
    public List<Comment> allComment() {
        String sql="select comment_id commentId,comment_publisher commentPublisher,comment_content commentContent,topic_name topicName from comment";
        List<Comment> allComments = SearchUtils.searchForList(Comment.class, sql);
        return allComments;
    }
}
